package com.dimples.security.entity;

import com.dimples.common.constant.StrPool;
import com.dimples.common.entity.SecurityUser;
import com.dimples.common.enums.LoginType;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;

import java.util.Collection;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/6/25
 */
@Setter
@Getter
public class SecurityUserDetails extends User {

    private LoginType loginType = LoginType.normal;

    private String realName;

    private String remoteAddress;

    private String theme;

    private String avatar;

    private Long userId;

    private Long deptId;

    private String email;

    private String mobile;

    private String ssex;

    private String password;

    private String loginTime;

    public SecurityUserDetails(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
    }

    public SecurityUserDetails(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
    }

    public static SecurityUserDetails convert(SecurityUser securityUser) {
        SecurityUserDetails userDetails = new SecurityUserDetails(securityUser.getUsername(), "", AuthorityUtils.commaSeparatedStringToAuthorityList(StrUtil.join(StrPool.COMMA, securityUser.getAuthorities())));
        userDetails.setRemoteAddress(securityUser.getRemoteAddress());
        userDetails.setRealName(securityUser.getRealName());
        userDetails.setTheme(securityUser.getTheme());
        userDetails.setAvatar(securityUser.getAvatar());
        userDetails.setUserId(securityUser.getUserId());
        userDetails.setEmail(securityUser.getEmail());
        userDetails.setMobile(securityUser.getMobile());
        userDetails.setSsex(securityUser.getSsex());
        userDetails.setLoginTime(securityUser.getLoginTime());
        return userDetails;
    }
}
